﻿
window.onload = function () {
    $('#loading-mask').fadeOut();
}
function nofindImg() {
    var img = event.srcElement;
    img.src = "../Images/ie.gif";
    img.onerror = null; //控制onerror事件只触发一次
}

//初始化左侧菜单
function InitLeftMenuComm() {
    $("#nav").accordion({ animate: false, fit: true, border: false });
    var selectedPanelname = '';
    $.each(_menus.menus, function (i, n) {
        var menulist = '';
        menulist += '<ul class="navlist">';
        $.each(n.menus, function (j, o) {
            var icon_path = o.icon;
            if (icon_path.indexOf("/WF/") > -1) {
                icon_path = o.icon;
            } else if (icon_path.indexOf("//") > -1) {
                icon_path = o.icon.subString(0);
            }
            //给待办添加数量
            if (o.menuname == "待办") {
                menulist += '<li><div ><a ref="' + o.menuid + '" href="#" rel="' + o.url + '" ><img border="0" src="' + icon_path + '" onerror=\'nofindImg();\' />&nbsp;<span class="nav" id="empworkCount">' + o.menuname + '</span></a></div> ';
            } else if (o.menuname == "抄送") {
                menulist += '<li><div ><a ref="' + o.menuid + '" href="#" rel="' + o.url + '" ><img border="0" src="' + icon_path + '" onerror=\'nofindImg();\' />&nbsp;<span class="nav" id="ccsmallCount">' + o.menuname + '</span></a></div> ';
            } else {
                menulist += '<li><div ><a ref="' + o.menuid + '" href="#" rel="' + o.url + '" ><img border="0" src="' + icon_path + '" onerror=\'nofindImg();\' />&nbsp;<span class="nav">' + o.menuname + '</span></a></div> ';
            }
            //  menulist += '<li><div ><a ref="' + o.menuid + '" href="#" rel="' + o.url + '" ><span class="icon ' + o.icon + '" >&nbsp;</span><span class="nav">' + o.menuname + '</span></a></div> ';
            if (o.child && o.child.length > 0) {
                //li.find('div').addClass('icon-arrow');
                menulist += '<ul class="third_ul">';
                $.each(o.child, function (k, p) {
                    icon_path = p.icon;

                    if (icon_path.indexOf("/WF/") > -1) {
                        icon_path = p.icon;
                    } else if (icon_path.indexOf("//") > -1) {
                        icon_path = p.icon.subString(0);
                    }
                    menulist += '<li><div><a ref="' + p.menuid + '" href="#" rel="' + p.url + '" ><img border="0" src="' + icon_path + '" onerror=\'nofindImg();\' />&nbsp;<span class="nav">' + p.menuname + '</span></a></div> </li>'
                    // menulist += '<li><div><a ref="' + p.menuid + '" href="#" rel="' + p.url + '" ><span class="icon ' + p.icon + '" >&nbsp;</span><span class="nav">' + p.menuname + '</span></a></div> </li>'
                });
                menulist += '</ul>';
            }

            menulist += '</li>';
        })
        menulist += '</ul>';
        $('#nav').accordion('add', {
            title: n.menuname,
            content: menulist,
            border: false,
            iconCls: 'icon-' + n.icon
        });

        if (i == 0)
            selectedPanelname = n.menuname;

    });
    $('#nav').accordion('select', selectedPanelname);



    $('.navlist li a').click(function () {
        var tabTitle = $(this).children('.nav').text();

        var url = $(this).attr("rel");
        var menuid = $(this).attr("ref");
        var icon = $(this).find('.icon').attr('class');

        var third = find(menuid);
        if (third && third.child && third.child.length > 0) {
            $('.third_ul').slideUp();

            var ul = $(this).parent().next();
            if (ul.is(":hidden"))
                ul.slideDown();
            else
                ul.slideUp();
        }
        else {
            addTab(tabTitle, url, icon);
            $('.navlist li div').removeClass("selected");
            $(this).parent().addClass("selected");
        }
    }).hover(function () {
        $(this).parent().addClass("hover");
    }, function () {
        $(this).parent().removeClass("hover");
    });



    function othermenu() {
        var tabTitle = $(this).children('.nav').text();

        var url = $(this).attr("rel");
        var menuid = $(this).attr("ref");
        var icon = $(this).find('.icon').attr('class');

        var third = find(menuid);
        if (third && third.child && third.child.length > 0) {
            $('.third_ul').slideUp();

            var ul = $(this).parent().next();
            if (ul.is(":hidden"))
                ul.slideDown();
            else
                ul.slideUp();



        }
        else {
            addTab(tabTitle, url, icon);
            $('.navlist li div').removeClass("selected");
            $(this).parent().addClass("selected");
        }
    }
    $('.panel-header div').click(function () {
        $('.third_ul').slideDown();
    });
    //选中第一个
    //var panels = $('#nav').accordion('panels');
    //var t = panels[0].panel('options').title;
    //$('#nav').accordion('select', t);
}

//初始化左侧
function InitLeftMenu() {
    $("#nav").accordion({ animate: false, fit: true, border: false });
    var selectedPanelname = '';
    var menulist = '<ul class="tree easyui-tree" data-options="animate:true,lines:true">';
    $.each(_menus.menus, function (i, n) {
        var icon_pathn = n.icon;
        if (icon_pathn.indexOf("/WF/") > -1) {
            icon_pathn = n.icon;
        } else if (icon_pathn.indexOf("//") > -1) {
            icon_pathn = n.icon.subString(0);
        }

        menulist += '<li><div ><a ref="' + n.menuid + '" href="#" rel="' + n.url + '" ><img border="0" src="' + icon_pathn + '" onerror=\'nofindImg();\' />&nbsp;<span>' + n.menuname + '</span></a></div> ';


        $.each(n.menus, function (j, o) {

            menulist += '<ul style="display:none">';
            var icon_path = o.icon;

            if (icon_path.indexOf("/WF/") > -1) {
                icon_path = o.icon;
            } else if (icon_path.indexOf("//") > -1) {
                icon_path = o.icon.subString(0);
            }

            //给待办添加数量
            if (o.menuname == "待办") {
                menulist += '<li><div ><a ref="' + o.menuid + '" href="#" rel="' + o.url + '" ><img border="0" src="' + icon_path + '" onerror=\'nofindImg();\' />&nbsp;<span class="nav" id="empworkCount">' + o.menuname + '</span></a></div> ';
            } else if (o.menuname == "抄送") {
                menulist += '<li><div ><a ref="' + o.menuid + '" href="#" rel="' + o.url + '" ><img border="0" src="' + icon_path + '" onerror=\'nofindImg();\' />&nbsp;<span class="nav" id="ccsmallCount">' + o.menuname + '</span></a></div> ';
            } else {
                menulist += '<li><div ><a ref="' + o.menuid + '" href="#" rel="' + o.url + '" ><img border="0" src="' + icon_path + '" onerror=\'nofindImg();\' />&nbsp;<span class="nav">' + o.menuname + '</span></a></div> ';
            }
            if (o.child && o.child.length > 0) {

                menulist += '<ul style="display:none">';
                $.each(o.child, function (k, p) {
                    icon_path = p.icon;

                    if (icon_path.indexOf("/WF/") > -1) {
                        icon_path = p.icon;
                    } else if (icon_path.indexOf("://") == -1) {
                        icon_path = "img/icon/" + p.icon;
                    }

                    menulist += '<li><div><a ref="' + p.menuid + '" href="#" rel="' + p.url + '" ><img border="0" src="' + icon_path + '" onerror=\'nofindImg();\' />&nbsp;<span class="nav">' + p.menuname + '</span></a></div>'
                    menulist += GetChild(true, p, "", 0);
                    menulist += '</li>';
                });
                menulist += '</ul>';
            }
            menulist += '</li>';
            menulist += '</ul>';
        })
        menulist += '</li>';


        //        $('#nav').accordion('add', {
        //            title: n.menuname,
        //            content: menulist,
        //            border: false,
        //            iconCls: 'icon-' + n.icon
        //        });

        //        if (i == 0)
        //            selectedPanelname = n.menuname;


    });
    menulist += '</ul>';

    $('#nav').accordion('add', {
        title: "功能列表",
        content: menulist,
        border: false
    });


    //$('#nav').accordion('select', selectedPanelname);



    $('#nav li a').click(function () {
        var tabTitle = $(this).children('.nav').text();

        var url = $(this).attr("rel");
        var menuid = $(this).attr("ref");
        var icon = $(this).find('.icon').attr('class');

        //        var third = find(menuid);
        //        if (third && third.child && third.child.length > 0) {
        //            $('.third_ul').slideUp();

        //            var ul = $(this).parent().next();
        //            if (ul.is(":hidden"))
        //                ul.slideDown();
        //            else
        //                ul.slideUp();
        //        }
        //        else {
        addTab(tabTitle, url, icon);
        $('.navlist li div').removeClass("selected");
        $(this).parent().addClass("selected");
        //}
    }).hover(function () {
        $(this).parent().addClass("hover");
    }, function () {
        $(this).parent().removeClass("hover");
    });




    function othermenu() {
        var tabTitle = $(this).children('.nav').text();

        var url = $(this).attr("rel");
        var menuid = $(this).attr("ref");
        var icon = $(this).find('.icon').attr('class');

        var third = find(menuid);
        if (third && third.child && third.child.length > 0) {
            $('.third_ul').slideUp();

            var ul = $(this).parent().next();
            if (ul.is(":hidden"))
                ul.slideDown();
            else
                ul.slideUp();



        }
        else {
            addTab(tabTitle, url, icon);
            $('.navlist li div').removeClass("selected");
            $(this).parent().addClass("selected");
        }
    }
    $('.panel-header div').click(function () {
        $('.third_ul').slideDown();
    });
    //选中第一个
    //var panels = $('#nav').accordion('panels');
    //var t = panels[0].panel('options').title;
    //$('#nav').accordion('select', t);
}

function GetChild(treeOrList, o, menulist, inx) {
    var menulist2 = menulist;
    if (o.child && o.child.length > 0) {
        menulist2 = "";
        inx++;
        if (treeOrList)
            menulist += '<ul style="display:none">';

        else
            menulist += '<ul class="third_ul">';
        $.each(o.child, function (k, p) {
            icon_path = p.icon;

            if (icon_path.indexOf("/WF/") > -1) {
                icon_path = p.icon;
            } else if (icon_path.indexOf("://") == -1) {
                icon_path = "img/icon/" + p.icon;
            }

            menulist += '<li><div><a ref="' + p.menuid + '" href="#" rel="' + p.url + '" ><img border="0" src="' + icon_path + '" onerror=\'nofindImg();\' />&nbsp;<span class="nav">' + p.menuname + '</span></a></div>'

            menulist2 += GetChild(treeOrList, p, menulist, inx);
            menulist2 += '</li>';
        });
        menulist2 += '</ul>';
    }
    return menulist2;
}

//获取左侧导航的图标
function getIcon(menuid) {
    var icon = 'icon ';
    $.each(_menus.menus, function (i, n) {
        $.each(n.menus, function (j, o) {
            if (o.menuid == menuid) {
                icon += o.icon;
            }
        })
    })

    return icon;
}

function find(menuid) {
    var obj = null;
    $.each(_menus.menus, function (i, n) {
        $.each(n.menus, function (j, o) {
            if (o.menuid == menuid) {
                obj = o;
            }
        });
    });

    return obj;
}

function addTab(subtitle, url, icon) {

    //将    创建流程  菜单 以弹出窗口的形式
    if (subtitle == "创建流程") {
        window.open(url)
        return;
    }
    if (!$('#tabs').tabs('exists', subtitle)) {
        $('#tabs').tabs('add', {
            title: subtitle,
            content: createFrame(url),
            closable: true,
            icon: icon
        });
    } else {
        $('#tabs').tabs('close', subtitle);
        $('#tabs').tabs('add', {
            title: subtitle,
            content: createFrame(url),
            closable: true,
            icon: icon
        });
        //$('#tabs').tabs('select',subtitle);
        //$('#mm-tabupdate').click();
    }
    tabClose();
}

function createFrame(url) {
    var s = '<iframe scrolling="auto" frameborder="0"  src="' + url + '" style="width:100%;height:100%;"></iframe>';
    return s;
}

function tabClose() {
    /*双击关闭TAB选项卡*/
    $(".tabs-inner").dblclick(function () {
        var subtitle = $(this).children(".tabs-closable").text();
        $('#tabs').tabs('close', subtitle);
    })
    /*为选项卡绑定右键*/
    $(".tabs-inner").bind('contextmenu', function (e) {
        $('#mm').menu('show', {
            left: e.pageX,
            top: e.pageY
        });

        var subtitle = $(this).children(".tabs-closable").text();

        $('#mm').data("currtab", subtitle);
        $('#tabs').tabs('select', subtitle);
        return false;
    });
}
//绑定右键菜单事件
function tabCloseEven() {
    //刷新
    $('#mm-tabupdate').click(function () {
        var currTab = $('#tabs').tabs('getSelected');
        var url = $(currTab.panel('options').content).attr('src');
        $('#tabs').tabs('update', {
            tab: currTab,
            options: {
                content: createFrame(url)
            }
        })
    })
    //关闭当前
    $('#mm-tabclose').click(function () {
        var currtab_title = $('#mm').data("currtab");
        $('#tabs').tabs('close', currtab_title);
    })
    //全部关闭
    $('#mm-tabcloseall').click(function () {
        $('.tabs-inner span').each(function (i, n) {
            var t = $(n).text();
            $('#tabs').tabs('close', t);
        });
    });
    //关闭除当前之外的TAB
    $('#mm-tabcloseother').click(function () {
        $('#mm-tabcloseright').click();
        $('#mm-tabcloseleft').click();
    });
    //关闭当前右侧的TAB
    $('#mm-tabcloseright').click(function () {
        var nextall = $('.tabs-selected').nextAll();
        if (nextall.length == 0) {
            //msgShow('系统提示','后边没有啦~~','error');
            //alert('后边没有啦~~');
            return false;
        }
        nextall.each(function (i, n) {
            var t = $('a:eq(0) span', $(n)).text();
            $('#tabs').tabs('close', t);
        });
        return false;
    });
    //关闭当前左侧的TAB
    $('#mm-tabcloseleft').click(function () {
        var prevall = $('.tabs-selected').prevAll();
        if (prevall.length == 0) {
            //alert('到头了，前边没有啦~~');
            return false;
        }
        prevall.each(function (i, n) {
            var t = $('a:eq(0) span', $(n)).text();
            $('#tabs').tabs('close', t);
        });
        return false;
    });

    //退出
    $("#mm-exit").click(function () {
        $('#mm').menu('hide');
    })
}

function refresh() {
    var currTab = $('#tabs').tabs('getSelected');
    var url = $(currTab.panel('options').content).attr('src');
    $('#tabs').tabs('update', {
        tab: currTab,
        options: {
            content: createFrame(url)
        }
    })
}

//弹出信息窗口 title:标题 msgString:提示信息 msgType:信息类型 [error,info,question,warning]
function msgShow(title, msgString, msgType) {
    $.messager.alert(title, msgString, msgType);
}